#include<stdio.h>
#include<at89c51ed2.h>
#include<mcs51reg.h>
#include<malloc.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>


#include "io.h"
#include "eeprom.h"
#include "errCheck.h"
#include "lcd.h"

/*****************************************************************************/
/**Critical Initialisation. Serial Communication at 19200 baud, XRAM 
 * 1024 bytes activated. This function has reference on Page 41 of 
 * the SDCC Manual*/
void _sdcc_external_startup() {
	AUXR|=0x0C;	/*XRAM 1024 bytes activated*/
	TMOD|=0x20;	/*Timer 1 Mode 2*/
	TH1=0xFD;	/*Timer Value 253 for baud rate 9600*/
	SCON = 0x50;/*SCON: mode 1, 8-bit UART, enable receiver*/
	PCON|=0x80;	/*double baud rate to 19200*/
	TR1 = 1; 	/*Start Timer 1 */
	TI = 1;		/*set TI flag for putchar sequence*/
	RI = 0;		/*clear RI flag for getchar sequence*/
	
}
/*****************************************************************************/
#define HEAP_SIZE 1600	/*heap size of 1600*/
unsigned char xdata heap[HEAP_SIZE];



void lcdTest();








void main()
{
char hold;
char choice; 
int i; 
int input;
xdata char * value;
int * hexD;
char t;	
xdata char * misc;
int rowChanger;
////write byte
int address;
int data_value;
int ack;
int lcd_len;
int space;
int initial;
/*initialisation functions*/
	init_dynamic_memory((MEMHEADER xdata *)heap, HEAP_SIZE);


misc=(xdata char *)malloc(10);
lcdinit();

//value=(xdata char *)malloc(10);
rowChanger=0;
initial=0;
while(1){
	
	
	
	
	
	
	clrScr();
	if(initial==0){
	lcdgotoxy('1','4');
	lcdputstr("Welcome");
	
	
	}
	printf_tiny("Enter an option to use the following functionality\n\r");
	printf_tiny("1 : Write Byte\t\t\t 2: Read Byte\n\r");
	printf_tiny("3 : LCD Display\t\t\t 4: Hex Dump(Random Read)\n\r");
	printf_tiny("5 : LCD Test\t\t\t\n\r ");
	choice=getchar();
	/*6: PCF8574 I/O Expander*/
	
	if(initial==0){
	lcdData(0x01,0);
	lcddelay(500);
	}
	initial++;
	
switch(choice){
	
case '1':	
	
	userInAddr: value=(xdata char *)malloc(30);
	newLine();
	printf_tiny("Please enter any address location between 00 to 7FF\n\rExample for location 0x40, type 40\n\r");
	gets(value);
	
	
	if(strlen(value)>4){
		printf_tiny("Please do not enter more than four characters\n\r");
		goto userInReadAddrLCD;
	}
	
	if(validHexInput(value)==0){
		printf_tiny("You have entered non-hexdecimal characters, please enter hexadecimal values (0-F) only\n\r");
	
	goto userInAddr;
	}
	
	input=lcd_asciiInput(value);
	
	
	
	if(input<0||input>2047){
	printf_tiny("That is beyond range, enter between 00 to 7FF\n\r");
	
	goto userInAddr;
	}
	
	
	//assigning address i2c
	address=input;
	/*********************************************************************************************/
	

	//value=(char *)malloc(10);
	userInData:value=(xdata char *)malloc(30);
	printf("Please enter data between 00 to FF that you want to write at location %04X\n\r For example, for 0xFF type FF\n\r",address);
	
	gets(value);
	
	if(strlen(value)>4){
		printf_tiny("Please do not enter more than four characters\n\r");
		goto userInReadAddrLCD;
	}
	
	if(validHexInput(value)==0){
		printf_tiny("You have entered non-hexdecimal characters, please enter hexadecimal (0-F) values only\n\r");
	
	 goto userInData;
	}
	
	input=lcd_asciiInput(value);
	

	
	//printf("Data %d",input);
	if(input<0||input>255){
	printf_tiny("That is beyond range, enter between 00 to FF\n\r");
	
	goto userInData;
	}
	
	
	//assigning address i2c
	data_value=input;
	/*********************************************************************************************/
	//printf("Control will now write %X at address location %X\n\r",data_value,address);
	
	ack=eebytew(address,data_value);
	if(ack==0){
		printf("Data %02X Stored Successfully at Location %04X\n\r",data_value,address);
	}
	else
	{
		printf_tiny("Data Storage Failed\n\r");
		goto userInData;
	}
	printf_tiny("Press any key to go back to Main Menu\n\r");
	hold=getchar();
	break;
/****************************************************************************************************/
	

	/*byter*/

case '2':		

	userInReadAddr:value=(xdata char *)malloc(10);
	newLine();
	printf_tiny("Please enter any address location between 00 to 7FF you wish to read at\n\r");
	
	gets(value);

	if(strlen(value)>4){
		printf_tiny("Please do not enter more than four characters\n\r");
		goto userInReadAddrLCD;
	}
	
	
	if(validHexInput(value)==0){
		printf_tiny("You have entered non-hexdecimal characters, please enter hexadecimal values only\n\r");
	
	goto userInReadAddr;
	}
	
	input=lcd_asciiInput(value);
	

	
	
	if(input<0||input>2047){
	printf_tiny("That is beyond range, enter between 00 to 7FF\n\r");
	
	goto userInReadAddr;
	}
	
	
	
	
	
	address=input;
	data_value=eebyter(address);
	
	printf("Data at location %04X : %02X\n\r",address,data_value);
	printf_tiny("\t\t\tPress any key to go back to Main Menu\n\r");
	hold=getchar();
	break;

	/*********************************************************************************************/
	/*lcd display*/
case '3':	
		

	userInReadAddrLCD: value=(xdata char *)malloc(10);

	newLine();
	printf_tiny("\t\t\t\tLCD Display\n\rPlease enter any address location between 00 to 7FF \n\rPlease enter any address location between 00 to 7FF\n\rExample for location 0x40, type 40\n\r");
	
	gets(value);
	if(strlen(value)>4){
		printf_tiny("Please do not enter more than four characters\n\r");
		goto userInReadAddrLCD;
	}
	
	if(validHexInput(value)==0){
		printf_tiny("You have entered non-hexdecimal characters, please enter hexadecimal values only\n\r");
	
	goto userInReadAddrLCD;
	}
	
	input=lcd_asciiInput(value);
	
	
	
	if(input<0||input>2047){
	printf_tiny("That is beyond range, enter between 00 to 7FF\n\r");
	
	goto userInReadAddrLCD;
	}
	
	lcd_len=4-strlen(value);

	
	
	address=input;
	data_value=eebyter(address);
	


	if(rowChanger>3){
		lcdgotoxy('0','0');
		rowChanger=0;
	}
	else if(rowChanger==1){
		lcdgotoxy('1','0');
	}
	else if(rowChanger==2){
		lcdgotoxy('2','0');
	}
	else if(rowChanger==3){
		lcdgotoxy('3','0');
	}
	rowChanger=rowChanger+1;
	
	_itoa(input,value,16);
	lcd_len=4-strlen(value);
	for(i=0;i<lcd_len;i++){
		lcdputstr("0");
	}
	
	stringtoUpper(value);
	lcdputstr(value);
	lcdputstr(":");
	_itoa(data_value,misc,16);

	if(strlen(misc)==1){
		lcdputstr("0");}
	lcdputstr(misc);
	printf_tiny("Data Now on LCD\n\r");
	

	printf_tiny("\t\t\tPress any key to go back to Main Menu\n\r");
	hold=getchar();
	break;

	
///*************************************************************************************/
///*hex dump*/
case '4': 
newLine();
//i=0;
//printf_tiny("\n\rDo you want repeated random reads or sequential read?\n\r");
//printf_tiny("\n\rPress 8 for Random , 9 for Sequential Read \n\r");



i=0;
printf_tiny("Commencing Hex Dump\n\r");
newLine();
address=0;
printf("|%04X: ",address);
address=2048;
space=0;

	while (i<address){
		if (space==16){
			printf_tiny("|");
			printf_tiny("\r\n"); /*functionality to go to next line*/
			printf("|%04X: ",i);
			space=0;
		}
		
		printf("%02X  ",eebyter(i)); /*Print Buffer Contents*/
		delay(10);
		i++;
		space++;
		
		}
	
	printf_tiny("\n\rPress any key to go back to Main Menu\n\r");
	hold=getchar();
	break;
/****************************************************************************************************/	
case '5':	clrScr();
			lcdData(0x01,0);
			lcddelay(500);		
			lcdTest();
			break;
	
/*****************************************************************************************************/
//case '6': 
//
//
//newLine();
//i=0;
//printf_tiny("Commencing Hex Dump\n\r");
//newLine();
//address=0;
//hexD=eebyteSeqr(0,2048);
//printf("|%04X: ",address);
//address=2048;
//space=0;
//
//	while (i<address){
//		if (space==16){
//			printf_tiny("|");
//			printf_tiny("\r\n"); /*functionality to go to next line*/
//			printf("|%04X: ",i);
//			space=0;
//		}
//		
//		printf("%02X  ",hexD[i]); /*Print Buffer Contents*/
//		delay(10);
//		i++;
//		space++;
//		
//		}
//	
//	printf_tiny("\n\rPress any key to go back to Main Menu\n\r");
//	hold=getchar();
//	break;
	
	default: printf_tiny("\n\rEnter the correct options please\n\r");
	
			printf_tiny("\n\rPress any key to go back to Main Menu\n\r");
			hold=getchar();
	
	
	
	
	
	}
	
	
	
	free(value);
		}
  }
//end of switch case
	
//end of the infinite while loop


	











